﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GeneralLibs;
using System.Data.SqlClient;

namespace Lists.SOP
{
    public class KhoanMucSOP : DoiTuong
    {
        #region Guid MainSOPID property
        public Guid MainSOPID
        {
            get
            {
                if (_DataRow["MainSOPID"] is Guid) return (Guid)_DataRow["MainSOPID"];
                return Guid.Empty;
            }
            set
            {
                _DataRow["MainSOPID"] = value;
            }
        }
        #endregion

        #region string HoTen property
        public string HoTen
        {
            get
            {
                if (_DataRow["HoTen"] is string) return (string)_DataRow["HoTen"];
                return "";
            }
            set
            {
                _DataRow["HoTen"] = value;
            }
        }
        #endregion

        #region int NamSinh property
        public int NamSinh
        {
            get
            {
                if (_DataRow["NamSinh"] is int) return (int)_DataRow["NamSinh"];
                return 0;
            }
            set
            {
                _DataRow["NamSinh"] = value;
            }
        }
        #endregion

        #region bool? GioiTinh property
        public bool? GioiTinh
        {
            get
            {
                if (_DataRow["GioiTinh"] is bool) return (bool)_DataRow["GioiTinh"];
                return null;
            }
            set
            {
                _DataRow["GioiTinh"] = value;
            }
        }
        #endregion

        #region string DiaChi property
        public string DiaChi
        {
            get
            {
                if (_DataRow["DiaChi"] is string) return (string)_DataRow["DiaChi"];
                return "";
            }
            set
            {
                _DataRow["DiaChi"] = value;
            }
        }
        #endregion

        #region string TrinhDoCM property
        public string TrinhDoCM
        {
            get
            {
                if (_DataRow["TrinhDoCM"] is string) return (string)_DataRow["TrinhDoCM"];
                return "";
            }
            set
            {
                _DataRow["TrinhDoCM"] = value;
            }
        }
        #endregion

        #region string NamTotNghiep property
        public string NamTotNghiep
        {
            get
            {
                if (_DataRow["NamTotNghiep"] is string) return (string)_DataRow["NamTotNghiep"];
                return "";
            }
            set
            {
                _DataRow["NamTotNghiep"] = value;
            }
        }
        #endregion

        #region string CongViecPhanCong property
        public string CongViecPhanCong
        {
            get
            {
                if (_DataRow["CongViecPhanCong"] is string) return (string)_DataRow["CongViecPhanCong"];
                return "";
            }
            set
            {
                _DataRow["CongViecPhanCong"] = value;
            }
        }
        #endregion

        #region ThuTuKhoanMuc property
        public int ThuTuKhoanMuc
        {
            get
            {
                if (_DataRow["ThuTuKhoanMuc"] is int)
                    return (int)_DataRow["ThuTuKhoanMuc"];
                else
                    return 0;
            }
            set
            {
                _DataRow["ThuTuKhoanMuc"] = value;
            }
        }
        #endregion

        #region DateTime? DateGeneral property
        public DateTime? DateGeneral
        {
            get
            {
                if (DataRow["DateGeneral"] is DateTime)
                    return (DateTime)DataRow["DateGeneral"];
                else
                    return null;
            }
            set
            {
                if (value is DateTime)
                    DataRow["DateGeneral"] = (DateTime)value;
                else
                    DataRow["DateGeneral"] = System.DBNull.Value;
            }
        }
        #endregion

        #region string HoTenBacSy property
        public string HoTenBacSy
        {
            get
            {
                if (_DataRow["HoTenBacSy"] is string) return (string)_DataRow["HoTenBacSy"];
                return "";
            }
            set
            {
                _DataRow["HoTenBacSy"] = value;
            }
        }
        #endregion

        #region string ChuanDoanBS property
        public string ChuanDoanBS
        {
            get
            {
                if (_DataRow["ChuanDoanBS"] is string) return (string)_DataRow["ChuanDoanBS"];
                return "";
            }
            set
            {
                _DataRow["ChuanDoanBS"] = value;
            }
        }
        #endregion

        #region string MaThuoc property
        public string MaThuoc
        {
            get
            {
                if (_DataRow["MaThuoc"] is string) return (string)_DataRow["MaThuoc"];
                return "";
            }
            set
            {
                _DataRow["MaThuoc"] = value;
            }
        }
        #endregion

        #region string TenThuoc property
        public string TenThuoc
        {
            get
            {
                if (_DataRow["TenThuoc"] is string) return (string)_DataRow["TenThuoc"];
                return "";
            }
            set
            {
                _DataRow["TenThuoc"] = value;
            }
        }
        #endregion

        #region decimal SLBan property
        public decimal SLBan
        {
            get
            {
                if (_DataRow["SLBan"] is decimal) return (decimal)_DataRow["SLBan"];
                return 0;
            }
            set
            {
                _DataRow["SLBan"] = value;
            }
        }
        #endregion

        #region decimal SLMua property
        public decimal SLMua
        {
            get
            {
                if (_DataRow["SLMua"] is decimal) return (decimal)_DataRow["SLMua"];
                return 0;
            }
            set
            {
                _DataRow["SLMua"] = value;
            }
        }
        #endregion

        #region string ThuocNghiADR property
        public string ThuocNghiADR
        {
            get
            {
                if (_DataRow["ThuocNghiADR"] is string) return (string)_DataRow["ThuocNghiADR"];
                return "";
            }
            set
            {
                _DataRow["ThuocNghiADR"] = value;
            }
        }
        #endregion

        #region int LanSuDung property
        public int LanSuDung
        {
            get
            {
                if (_DataRow["LanSuDung"] is int) return (int)_DataRow["LanSuDung"];
                return 0;
            }
            set
            {
                _DataRow["LanSuDung"] = value;
            }
        }
        #endregion

        #region string HienTuongADR property
        public string HienTuongADR
        {
            get
            {
                if (_DataRow["HienTuongADR"] is string) return (string)_DataRow["HienTuongADR"];
                return "";
            }
            set
            {
                _DataRow["HienTuongADR"] = value;
            }
        }
        #endregion

        #region int LanHienTuong property
        public int LanHienTuong
        {
            get
            {
                if (_DataRow["LanHienTuong"] is int) return (int)_DataRow["LanHienTuong"];
                return 0;
            }
            set
            {
                _DataRow["LanHienTuong"] = value;
            }
        }
        #endregion

        #region string XuLy property
        public string XuLy
        {
            get
            {
                if (_DataRow["XuLy"] is string) return (string)_DataRow["XuLy"];
                return "";
            }
            set
            {
                _DataRow["XuLy"] = value;
            }
        }
        #endregion

        #region string LyDo property
        public string LyDo
        {
            get
            {
                if (_DataRow["LyDo"] is string) return (string)_DataRow["LyDo"];
                return "";
            }
            set
            {
                _DataRow["LyDo"] = value;
            }
        }
        #endregion

        #region string SoKSHanDung property
        public string SoKSHanDung
        {
            get
            {
                if (_DataRow["SoKSHanDung"] is string) return (string)_DataRow["SoKSHanDung"];
                return "";
            }
            set
            {
                _DataRow["SoKSHanDung"] = value;
            }
        }
        #endregion

        #region string NSX property
        public string NSX
        {
            get
            {
                if (_DataRow["NSX"] is string) return (string)_DataRow["NSX"];
                return "";
            }
            set
            {
                _DataRow["NSX"] = value;
            }
        }
        #endregion

        #region string ChatLuongCamQuan property
        public string ChatLuongCamQuan
        {
            get
            {
                if (_DataRow["ChatLuongCamQuan"] is string) return (string)_DataRow["ChatLuongCamQuan"];
                return "";
            }
            set
            {
                _DataRow["ChatLuongCamQuan"] = value;
            }
        }
        #endregion

        #region string Note property
        public string Note
        {
            get
            {
                if (_DataRow["Note"] is string) return (string)_DataRow["Note"];
                return "";
            }
            set
            {
                _DataRow["Note"] = value;
            }
        }
        #endregion

        #region decimal SLThuHoi property
        public decimal SLThuHoi
        {
            get
            {
                if (_DataRow["SLThuHoi"] is decimal) return (decimal)_DataRow["SLThuHoi"];
                return 0;
            }
            set
            {
                _DataRow["SLThuHoi"] = value;
            }
        }
        #endregion

        #region string NguoiThuHoi_ChuKy property
        public string NguoiThuHoi_ChuKy
        {
            get
            {
                if (_DataRow["NguoiThuHoi_ChuKy"] is string) return (string)_DataRow["NguoiThuHoi_ChuKy"];
                return "";
            }
            set
            {
                _DataRow["NguoiThuHoi_ChuKy"] = value;
            }
        }
        #endregion

        #region string SoCVDinhChi property
        public string SoCVDinhChi
        {
            get
            {
                if (_DataRow["SoCVDinhChi"] is string) return (string)_DataRow["SoCVDinhChi"];
                return "";
            }
            set
            {
                _DataRow["SoCVDinhChi"] = value;
            }
        }
        #endregion

        #region string SoLo property
        public string SoLo
        {
            get
            {
                if (_DataRow["SoLo"] is string) return (string)_DataRow["SoLo"];
                return "";
            }
            set
            {
                _DataRow["SoLo"] = value;
            }
        }
        #endregion

        #region DateTime? HSD property
        public DateTime? HSD
        {
            get
            {
                if (DataRow["HSD"] is DateTime)
                    return (DateTime)DataRow["HSD"];
                else
                    return null;
            }
            set
            {
                if (value is DateTime)
                    DataRow["HSD"] = (DateTime)value;
                else
                    DataRow["HSD"] = System.DBNull.Value;
            }
        }
        #endregion

        #region string NoiLayMau property
        public string NoiLayMau
        {
            get
            {
                if (_DataRow["NoiLayMau"] is string) return (string)_DataRow["NoiLayMau"];
                return "";
            }
            set
            {
                _DataRow["NoiLayMau"] = value;
            }
        }
        #endregion

        #region decimal SLTon property
        public decimal SLTon
        {
            get
            {
                if (_DataRow["SLTon"] is decimal) return (decimal)_DataRow["SLTon"];
                return 0;
            }
            set
            {
                _DataRow["SLTon"] = value;
            }
        }
        #endregion

        #region DateTime? NgayThuHoi property
        public DateTime? NgayThuHoi
        {
            get
            {
                if (DataRow["NgayThuHoi"] is DateTime)
                    return (DateTime)DataRow["NgayThuHoi"];
                else
                    return null;
            }
            set
            {
                if (value is DateTime)
                    DataRow["NgayThuHoi"] = (DateTime)value;
                else
                    DataRow["NgayThuHoi"] = System.DBNull.Value;
            }
        }
        #endregion

        #region string NDKhieuNai property
        public string NDKhieuNai
        {
            get
            {
                if (_DataRow["NDKhieuNai"] is string) return (string)_DataRow["NDKhieuNai"];
                return "";
            }
            set
            {
                _DataRow["NDKhieuNai"] = value;
            }
        }
        #endregion

        #region string TenCty property
        public string TenCty
        {
            get
            {
                if (_DataRow["TenCty"] is string) return (string)_DataRow["TenCty"];
                return "";
            }
            set
            {
                _DataRow["TenCty"] = value;
            }
        }
        #endregion

        #region string SDT property
        public string SDT
        {
            get
            {
                if (_DataRow["SDT"] is string) return (string)_DataRow["SDT"];
                return "";
            }
            set
            {
                _DataRow["SDT"] = value;
            }
        }
        #endregion

        #region string SoHoaDon property
        public string SoHoaDon
        {
            get
            {
                if (_DataRow["SoHoaDon"] is string) return (string)_DataRow["SoHoaDon"];
                return "";
            }
            set
            {
                _DataRow["SoHoaDon"] = value;
            }
        }
        #endregion

        #region string QuyCach property
        public string QuyCach
        {
            get
            {
                if (_DataRow["QuyCach"] is string) return (string)_DataRow["QuyCach"];
                return "";
            }
            set
            {
                _DataRow["QuyCach"] = value;
            }
        }
        #endregion

        #region string SoDangKy property
        public string SoDangKy
        {
            get
            {
                if (_DataRow["SoDangKy"] is string) return (string)_DataRow["SoDangKy"];
                return "";
            }
            set
            {
                _DataRow["SoDangKy"] = value;
            }
        }
        #endregion

        #region string SoKS property
        public string SoKS
        {
            get
            {
                if (_DataRow["SoKS"] is string) return (string)_DataRow["SoKS"];
                return "";
            }
            set
            {
                _DataRow["SoKS"] = value;
            }
        }
        #endregion

        #region string SoChungTu property
        public string SoChungTu
        {
            get
            {
                if (_DataRow["SoChungTu"] is string) return (string)_DataRow["SoChungTu"];
                return "";
            }
            set
            {
                _DataRow["SoChungTu"] = value;
            }
        }
        #endregion

        #region string DVT property
        public string DVT
        {
            get
            {
                if (_DataRow["DVT"] is string) return (string)_DataRow["DVT"];
                return "";
            }
            set
            {
                _DataRow["DVT"] = value;
            }
        }
        #endregion

        #region decimal? NgayNhietDo property
        public decimal? NgayNhietDo
        {
            get
            {
                if (_DataRow["NgayNhietDo"] is decimal) return (decimal)_DataRow["NgayNhietDo"];
                return null;
            }
            set
            {
                if (value is decimal)
                    DataRow["NgayNhietDo"] = (decimal)value;
                else
                    DataRow["NgayNhietDo"] = System.DBNull.Value;
            }
        }
        #endregion

        #region decimal? NhietDoSang property
        public decimal? NhietDoSang
        {
            get
            {
                if (_DataRow["NhietDoSang"] is decimal) return (decimal)_DataRow["NhietDoSang"];
                return null;
            }
            set
            {
                if (value is decimal)
                    DataRow["NhietDoSang"] = (decimal)value;
                else
                    DataRow["NhietDoSang"] = System.DBNull.Value;
            }
        }
        #endregion

        #region decimal? DoAmSang property
        public decimal? DoAmSang
        {
            get
            {
                if (_DataRow["DoAmSang"] is decimal) return (decimal)_DataRow["DoAmSang"];
                return null;
            }
            set
            {
                if (value is decimal)
                    DataRow["DoAmSang"] = (decimal)value;
                else
                    DataRow["DoAmSang"] = System.DBNull.Value;
            }
        }
        #endregion

        #region decimal? NhietDoChieu property
        public decimal? NhietDoChieu
        {
            get
            {
                if (_DataRow["NhietDoChieu"] is decimal) return (decimal)_DataRow["NhietDoChieu"];
                return null;
            }
            set
            {
                if (value is decimal)
                    DataRow["NhietDoChieu"] = (decimal)value;
                else
                    DataRow["NhietDoChieu"] = System.DBNull.Value;
            }
        }
        #endregion

        #region decimal? DoAmChieu property
        public decimal? DoAmChieu
        {
            get
            {
                if (_DataRow["DoAmChieu"] is decimal) return (decimal)_DataRow["DoAmChieu"];
                return null;
            }
            set
            {
                if (value is decimal)
                    DataRow["DoAmChieu"] = (decimal)value;
                else
                    DataRow["DoAmChieu"] = System.DBNull.Value;
            }
        }
        #endregion

        public KhoanMucSOP(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucKhoanMucSOP : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucKhoanMucSOP _Instance;
        public static DanhMucKhoanMucSOP Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucKhoanMucSOP();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                    _Instance.DataView.Sort = "ThuTuKhoanMuc DESC";
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "KhoanMucSOP";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new KhoanMucSOP((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public KhoanMucSOP this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new KhoanMucSOP(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public KhoanMucSOP NewDoiTuong()
        {
            return new KhoanMucSOP(DataTable.NewRow());
        }

        public KhoanMucSOP GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new KhoanMucSOP(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        public KhoanMucSOP GetByID(Guid KhoanMucSOPID)
        {
            return GetByProperty("ID", KhoanMucSOPID);
        }
        public KhoanMucSOP GetByMa(string ma)
        {
            try
            {
                DataRow[] rows = DataTable.Select(string.Format("Ma = '{0}'", ma));
                if (rows.Length > 0)
                    return new KhoanMucSOP(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucKhoanMucSOP GetAll()
        {
            return Instance;
        }

        public static DanhMucKhoanMucSOP GetByDanhMucMainSOP(DanhMucMainSOP dmMainSop)
        {
            string mainSOPCondition = "";
            foreach (MainSOP mainSOP in dmMainSop)
            {
                mainSOPCondition += "CONVERT('{" + mainSOP.ID + "}','System.Guid'), ";
            }
            if (mainSOPCondition.EndsWith(", ")) mainSOPCondition = mainSOPCondition.Remove(mainSOPCondition.Length - 2);
            string filterExpresstion = string.Format("MainSOPID IN ({0})", mainSOPCondition);
            return Filter(filterExpresstion);
        }

        public static DanhMucKhoanMucSOP GetByMainSOPID(Guid mainSOPID)
        {
            string mainSOPCondition = "CONVERT('{" + mainSOPID + "}','System.Guid') ";
            string filterExpresstion = string.Format("MainSOPID IN ({0})", mainSOPCondition);
            return Filter(filterExpresstion);
        }

        public static DanhMucKhoanMucSOP Filter(string filterExpression)
        {
            try
            {
                DanhMucKhoanMucSOP obj = new DanhMucKhoanMucSOP();
                obj.DataView = new DataView(DanhMucKhoanMucSOP.Instance.DataTable, filterExpression, "ThuTuKhoanMuc", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
        public static KhoanMucSOP Filter(string filterExpression, bool a)
        {
            try
            {
                DanhMucKhoanMucSOP obj = new DanhMucKhoanMucSOP();
                obj.DataView = new DataView(DanhMucKhoanMucSOP.Instance.DataTable, filterExpression, "ThuTuKhoanMuc", DataViewRowState.CurrentRows);
                return obj[0];
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public int GetNextThuTuKhoanMuc(Guid mainSOPID)
        {
            SqlConnection con = new SqlConnection(LocalSettings.ConnectString);
            try
            {
                con.Open();
                SqlCommand cmd = con.CreateCommand();
                return GetNextThuTuKhoanMuc(cmd, mainSOPID);    
            }
            catch (Exception ex)
            {
                return 0;
            }
        }

        public int GetNextThuTuKhoanMuc(SqlCommand cmd, Guid mainSOPID)
        {
            try
            {
                cmd.CommandText = string.Format("SELECT MAX(ThuTuKhoanMuc) ThuTu FROM KhoanMucSOP WHERE MainSOPID = '{0}'", mainSOPID);
                SqlDataAdapter da = new SqlDataAdapter();
                da.SelectCommand = cmd;
                DataTable dt = new DataTable();
                da.Fill(dt);
                if (dt != null && dt.Rows.Count >= 1 && dt.Rows[0]["ThuTu"] is int)
                    return ((int)dt.Rows[0]["ThuTu"] + 1);
                else
                    return 1; //Coi như bắt đầu
            }
            catch (Exception ex)
            {
                return 0;
            }
        }
    }
}
